package com.join;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.util.DBConn;

public class MemberServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public MemberServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		process(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		process(request, response);
	}

	protected void process(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("UTF-8");
		Connection conn = DBConn.getConnection();
		MemberDAO dao = new MemberDAO(conn);
		String cp = req.getContextPath();
		String uri = req.getRequestURI();
		String url;

		if (uri.indexOf("created.do") != -1) {
			url = "/member/created.jsp";
			forward(req, resp, url);
		} else if (uri.indexOf("created_ok.do") != -1) {
			MemberDTO dto = new MemberDTO();
			dto.setUserId(req.getParameter("userId"));
			dto.setUserPwd(req.getParameter("userPwd"));
			dto.setUserName(req.getParameter("userName"));
			dto.setUserBirth(req.getParameter("userBirth"));
			dto.setUserTel(req.getParameter("userTel"));
			dao.insertData(dto);

			url = cp;
			resp.sendRedirect(url);
		} else if (uri.indexOf("login.do") != -1) {
			url = "/member/login.jsp";
			forward(req, resp, url);
		} else if (uri.indexOf("login_ok.do") != -1) {
			String userId = req.getParameter("userId");
			String userPwd = req.getParameter("userPwd");

			MemberDTO dto = dao.getReadData(userId);
			if (dto == null || !dto.getUserPwd().equals(userPwd)) {
				req.setAttribute("message", "아이디 또는 패스워드가 틀립니다.");
				url = "/member/login.jsp";
				forward(req, resp, url);
				return;
			}
			HttpSession session = req.getSession(true);
			SessionInfo info = new SessionInfo();
			info.setUserId(dto.getUserId());
			info.setUserName(dto.getUserName());

			session.setAttribute("session", info);

			url = cp;
			resp.sendRedirect(url);
		} else if (uri.indexOf("logout.do") != -1) {
			HttpSession session = req.getSession();
			session.removeAttribute("session");
			session.invalidate();
			url = cp;
			resp.sendRedirect(url);
		}
	}

	protected void forward(HttpServletRequest req, HttpServletResponse resp,
			String url) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RequestDispatcher rd = req.getRequestDispatcher(url);
		rd.forward(req, resp);
	}
}
